<?php
namespace App\Http\Controllers\Api;

use App\Service\Model\Cards;
use App\Service\Model\OrderDetails;
use App\Service\Model\Orders;
use Brick\Math\RoundingMode;
use Illuminate\Http\Request;

class OrderController extends BaseController
{

    public function create(Request $request)
    {
        $cardno = $request->cardno;
        $bill_amount = $request->bill_amount; //账单金额
        $pay_date = $request->pay_date;
        $max_amount = $request->max_amount; //信用卡最大额度
        if ($max_amount < 100) {
            return $this->failed('信用卡最大刷卡额度不能低于100元');
        }

        if ($max_amount > 1000) {
            $max_amount = 999;
        }

        if ($bill_amount < 100 || $bill_amount > 30000) {
            return $this->failed('金额必须在100~30000之间');
        }

        $card = Cards::getCardByNo($cardno);
        if (empty($card)) {
            return $this->failed('此卡不存在');
        }
        if (empty($card->agreeid) || $card->status != Cards::status_success) {
            return $this->failed('此卡尚未成功绑定');
        }
        if ($card->plan_status == Cards::plan_status_on) {
            return $this->failed('已制定计划,请完成当前计划后再次制定');
        }

        $order = Orders::create([
            'uid' => user()->getKey(),
            'cusid' => user()->cusid,
            'agency_id' => user()->agency_id,
            'total_amount' => $bill_amount,
            'bill_amount' => $bill_amount,
            'pay_date' => $pay_date,
            'cardno' => $cardno,
            'bank_name' => $card->bank_name,
            'acctname' => $card->acctname,
            'agreeid' => $card->agreeid,
            'status' => Orders::status_init,
        ]);

        $orderDetails = OrderDetails::createOrderDetails($bill_amount, $card, $pay_date, $order->getKey(), $max_amount);
        if (!$orderDetails) {
            return $this->failed('您的还款日距离太近了,无法在最后还款日前完成执行!');
        }

        $order->pay_fee = toAmount(rmb($bill_amount)->multipliedBy('0.008', RoundingMode::UP));
        $order->withdraw_fee = $order->details()->where('type', 2)->count();
        $order->total_fee = toAmount(rmbPlus($order->pay_fee, $order->withdraw_fee));
        $order->plan_finish_date = $order->details->last()->execute_time;
        $order->save();
        $order->details();
        return $this->success($order);
    }

    /**
     * 确认生成计划
     *
     * @param Request $request
     * @return void
     */
    public function confirm(Request $request)
    {
        $order_id = $request->order_id;
        $cardno = Orders::findOrFail($order_id)->getOriginal('cardno');

        $order = Orders::findOrFail($order_id);
        $order->status = Orders::status_default;
        $order->save();

        $card = Cards::getCardByNo($cardno);
        $card->plan_status = Cards::plan_status_on;
        $card->save();
        return $this->success();
    }

    /**
     * 取消计划
     *
     * @param Request $request
     * @return boolean
     */
    public function cancel(Request $request)
    {
        $order_id = $request->order_id;
        /** @var Orders $order */
        $order = Orders::findOrFail($order_id);

        $order->details()->delete();
        $order->delete();
        return $this->success();
    }
    /**
     * 获取订单列表
     *
     * @param Request $request
     * @return void
     */
    public function orderLists(Request $request)
    {
        $user = user();
        if ($request->has('cardno') && $request->cardno != '') {
            $order = Orders::where('uid', $user->getKey())->whereIn('status', [Orders::status_default, Orders::status_success, Orders::status_failed])->where('cardno', $request->cardno)->orderBy('id','desc')->get()->toArray();
        } else {
            $order = Orders::where('uid', $user->getKey())->whereIn('status', [Orders::status_default, Orders::status_success, Orders::status_failed])->orderBy('id','desc')->get()->toArray();
        }
        return $this->success($order);
    }

    /**
     * 订单详情
     *
     * @param Request $request
     * @return void
     */
    public function orderDetails(Request $request)
    {
        $order_id = $request->order_id;
        $details = OrderDetails::where('order_id', $order_id)->orderBy('execute_time', 'asc')->get();
        return $this->success($details);
    }
}
